home *** CD-ROM | disk | FTP | other *** search
/ Hacker's Arsenal - The Cutting Edge of Hacking / Hacker's Arsenal - The Cutting Edge of Hacking.iso / texts / misc / eprogramming.txt < prev    next >
Encoding:
Text File  |  2001-07-11  |  16.7 KB  |  540 lines

  1. /*****************************************************
  2. Disclaimer:
  3. I take no responsibility for any damages the code in this tutorial
  4. May cause.  This code has been run on my machine and others, and
  5. Should not contain errors. For your own personal safety, virus scan
  6. It first, as you should all files on the net
  7. *****************************************************/
  8.  
  9. 111111111111111111111111111111111111111111111111111111111
  10. 100000000000000000000000000000000000000000000000000000001
  11. 100000000000000000000000000000000000000000000000000000001
  12. 10000 00000000000000     00000000000000000000000000000001
  13. 100 00 0 00000000000000 0000000000    00000 0     0000001
  14. 100 00 000 0000000 0000 00000000 00000 0000 0 000 0000001
  15. 100    0000 00000 00000 0000000 0000000 000 0 000 0000001
  16. 100 00 000000 00 000000 0000000 0000000 000 0 000 0000001
  17. 100 00 0000000  0000000 0000000 0000000 000  0000 0000001
  18. 100 00 0000000 00 00000 00000000 00000 0000 00000 0000001
  19. 1000000000000 00000 000 000000000 000 00000 00000 0000001
  20. 100000000000 000000000    00000000   000000 00000 0000001
  21. 10000000000 000000000000000000000000000000000000000000001
  22. 100000000000000000000000000000000000000000000000000000001
  23. 111111111111111111111111111111111111111111111111111111111
  24.  
  25. Computer Encryption, an overview and programming.
  26. TOPICS:
  27. -255
  28. 1.0    - Encryption what is it?
  29. 1.1    - Is encryption even worthy?
  30. 1.2    - Yeah yeah, how do I encrypt stuff?
  31. 1.3    - Programming - Fundamentals
  32. 1.3.1  - Binary
  33. 1.3.2  - XOR, AND, ONES COMPLIMENTRY -Logical operators
  34. 1.3.3  - Structured C Programming.
  35. 9.9.9  - Encryption Example
  36.  
  37.  
  38. -255 Why are you reading this?
  39.      Learn howto write efficient,structured encryption programs.
  40.      Some programming knowledge is required, languages C/C++ will be fine.
  41.      My homepage is: http://www.angelfire.com/sd/kias
  42.      or visit my small asm tutorial
  43.      http://www.angelfire.com/sd/kias/smart/
  44.  
  45.  
  46. 1.0 Encryption, What is it?
  47.  
  48. Encryption, what the hell?
  49. Yeah that's right, encryption. A Great Way to secure your privacy.
  50. Especially for all you l33t crack3rs out there.
  51. I know you want to jump to the programming so I'll just give you the
  52. Dictionary idea on what encryption is:
  53. 'Encrypt: 1. To put a message into code;
  54.            2. To put computer data into an encoded form
  55.            3. To get killed by a herd of killer monkeys.
  56.            4. yadadada
  57. '
  58. Ok obviously it's not 1 or 2, so lets look at some reasons why 3 and 4 are
  59. Accurate!
  60. Ok, you just got a new computer and your now a leet user right?
  61. You download all the tools but theres one that catches your eye..
  62. It asks you to enter the Filename of the file you want to encrypt so
  63. you do as it says, and pick your mums business file, work.dat
  64. it loads for a while and then says, encryption complete, you feel
  65. as though you could take on an army you feel leet.
  66.  
  67. Two days later, your mum is unable to complete her job for work and loses
  68. her job, you innocently deny the events that fucked up her datafile.
  69. But deep down you feel guilty, you hate encryption and never want to see
  70. it again.
  71.  
  72. Wrong! 'MOST' encryption programs are released with a 'de'Cryptor. !!
  73. You ARE able to turn mums work back into readable format!
  74. Unfortunately for this guy, he has no decryption and is banned from his
  75. computer for the rest of his teenage hood. Im not quite sure, but I think
  76. he got trampled by a herd of killer monkeys?
  77.  
  78. Anyway, sorry for my insolent humor. I figured no one would read this part
  79. so if you are, I'm sorry =(
  80. Ok, encryption turns your mums great work files into useless code that's
  81. Unreadable, ready to continue on?
  82.  
  83. 1.1 Is encryption even worthy?
  84.   Answer: No
  85.   Wrong Answer: It may come in handy if your cracking passwords etc or
  86.                 to protect email and stuff, but hey lets get with the times
  87.                 emails outdated.
  88.                 #35614954 for all you icq fans out there!
  89.  
  90. 1.2 Yeah yeah how do I encrypt stuff?
  91.  
  92. Its easy, download and run an encryption program. Parse all the correct
  93. parameters to the encryptor and WALA!
  94. If your using a good encryptor is should be able to DE-ENCRYPT in one!
  95. If you would like a FULLY WORKING ENCRYPTION PROGRAM refer to the example
  96. program at the bottom of this page!
  97.  
  98. 1.3 Programming Fundamentals!
  99.  
  100. Lets Pretend were writing an encryption program in 'English'
  101. it would look like this:
  102.  
  103.   ask for file1 to encrypt
  104.   ask for file2 to output encrypted data too
  105.   encrypt file1 and save as file2 leave file1 untouched
  106.  
  107.   Although this tutorial is C based, I will now show you a
  108.   visual basic example followed by a C example for all of you
  109.   without C experience.  Ohh and for all of you with no experience,
  110.   bye. Nah you can stay, maybe you'll learn something :)
  111.  
  112.   Visual Basic:
  113.     private sub commandbutton1_click()
  114.     dim file1,file2 as integer;     ` I know this is wrong, but
  115.     file1 = inputbox("File to Encrypt"); `were pretending...
  116.     file2 = inputbox("File to output to"); `remember????
  117.     encrypt
  118.     end sub
  119. END VISUAL Basic
  120.  
  121. C:
  122.    FILE *stream1,stream2;
  123.  
  124.    if((stream1 = fopen(argv[1], "rb")) == NULL) { /*ERROR*/ }
  125.    if((stream2 = fopen(argv[2], "wb")) == NULL) { /* ERROR*/}
  126.     encrypt(file1,file2);
  127.    END C!
  128.  
  129. Ok, if I have lost you. Read the next section otherwise skip it -=)
  130.  
  131. NExt Section:
  132.   stream1 and 2 are simply  pointers to of type FILE
  133.   argv[1] is the commandline parameter passed to programs in dos.
  134.   example:
  135.  
  136. program file1 file2 key
  137.   file1 is argv[1]
  138.   program is argv[0]
  139.   file2 is argv[2] and key is argv[3]
  140.   Ok so we know how to accept parameters! or do we? Here's an example
  141.   help you understand:
  142.  
  143.   #include <stdio.h>
  144.  
  145.    int main(int argc, char *argv[])
  146.    {
  147.       printf("Parameter 1 is %s, 2 is %s.\n", argv[1], argv[2]);
  148.       printf("Program name is %s.\n", argv[0];
  149.       printf("Number of parameters: %d\n", argc);
  150.     return 0;
  151.    }
  152. Ok if it doesn't make sense, compile it with you favorite C compiler.
  153. Note: If you don't have a compiler, you obviously don't know how to program 
  154. ?
  155. And this file may be a little extensive, programming wise for your liking.
  156. If you want keep reading feel free =)
  157.  
  158. Note: The best way to learn is to teach people, so go and teach all
  159. your friends how to use argv and argc you'll be the coolest kid in school!
  160. I promise.
  161.  
  162. 1.3.1 Binary
  163. Binary, hmmm and it would be?
  164. Answer: The only language the computer really understands.
  165. Although you may type some programs up and compile them, wether it be
  166. pascal,C,asm,delphi,vb,fortran or your dads billygoat your computer does
  167. not read what you tell it to, I know it sux, but its the truth.
  168. All your pretty little code is converted into a jumple of 1's and 0's.
  169. But you may protest, pfft what a load of shit, my programs contain advanced
  170. algorithms that couldnt possibly be defined with 1's and zero's. Wrong,
  171. every event which takes place on your computer is just a sequence of 
  172. commands
  173. Heres an example that will enlighten you a little, the typical hello world
  174. program:
  175. int main()
  176. {
  177.   while(1)
  178.   printf("Hello, World!\n");
  179.  
  180.   return 0;
  181. }
  182. This program is my enhancement to the original, this baby can produce 10000
  183. Instances of hello world in under 1 minute!
  184. Actually, it simply loops for ever displaying the inevitable. Lets look at
  185. what really happens.
  186. Warning, ASM to follow:
  187.  
  188. .data
  189. msg db "Hello, World!$",13,10 ; 13,10 is equivalent to \n
  190. .code
  191.     START:
  192.      mov ax, seg msg
  193.      mov ds, ax
  194.      mov dx, offset msg
  195.      mov ah, 09
  196.      int 21h
  197.      jmp START
  198.   END START
  199.  
  200. This program does exactly as the C program does, but you are able to see
  201. how it does what it does. Take attention to the jmp START instruction!
  202. Remember how a while loop loops until a condition is false? What you don't?
  203. Maybe that's because I haven't taught you yet. Here's the Catch. All it does
  204. is jmp to the start again, and executes all the instructions again!
  205. All this time you thought it involved magic etc. Pfft
  206.  
  207. But you start to speak, "This 'aint' binary dude", yes indeed its not.
  208. But as you are about to find out, Your cute little C hello world program
  209. is converted to ASM so windows can run it!
  210. Computers are seriously stupid and only do what they are told.
  211. Ok, your asm code is then converted to 1's and 0's so the processor can read
  212. it.  Do I see a pattern?
  213.  
  214.    Notepad.exe
  215.     hello.c
  216.      //code
  217.       compiler
  218.        asm
  219.         operating system
  220.          processor
  221.  
  222.  
  223. Ok, so now you see my point. Sorry i had to explain it for so long..
  224. Some people just don't understand. If you still don't believe me, please
  225. do a suicide mission. j/k
  226. : "I am hereby responsible for no suicides that take place directly after
  227.     the reading of this text"
  228. Now that we have that settled, lets learn some Binary!
  229.  
  230. BINARY NUMBER SYSTEM:
  231. Decimal - Binary
  232.     1         0001
  233.     2         0010
  234.     3         0011
  235.     4         0100
  236.     5         0101
  237.     6         0110
  238.     7         0111
  239.     8         1000
  240.     16       10000
  241.     32      100000
  242.     64     1000000
  243.     128   10000000
  244.     256  100000000
  245.  
  246. Character - Decimal - Binary
  247.          a        97        01100001
  248.          z       123        01111011
  249.  
  250. I'm sure you can figure the rest out.. :)
  251.  
  252. Repeat to yourself, I now know binary!
  253.  
  254. 1.3.2 Logical operator, AND, XOR, ONES COMPLIMENTARY
  255.  
  256. Ok, in this section you need to know binary, so please read the above 
  257. section
  258.  
  259. LOGICAL 'AND'
  260.        1 0
  261.     1  1 0
  262.     0  0 0
  263.  
  264. LOGICAL 'XOR'
  265.        1 0
  266.     1  0 1
  267.     0  1 0
  268.  
  269. ONES COMPLIMENT '~'
  270. Example-
  271. Decimal: 21845         Binary:          01010101 01010101
  272. ~Decimal: 42945450      Binary:          10101010 10101010
  273.  
  274. As above, we apply ~ to decimal 21845.
  275. It simply turns 1's to 0's and 0's to ones.
  276.  
  277. TIP: Xor is good for encryption because applying it with the old value it
  278. was XORED with turns the bits back into their previous value:
  279.  
  280. Example-
  281.  
  282. We have two characters:
  283. 'a' and 'b'
  284. We want to encrypt 'a' by XORING the bits of 'b' into it.
  285. Do this:
  286.  
  287.   int encrypted;
  288.   char a,b;
  289.   a = 'a';
  290.   b = 'b';
  291.  
  292.    encrypted = a ^ b;
  293.  
  294.   Which will yield this result:
  295.   'a' = 97 which is      01100001 in binary
  296.   'b' = 98 which is      01100010
  297.   encrypted now equals:  00000011
  298.  
  299.   But were learning how to decrypt as well right?
  300.   Sure are, to restore the value 'a' to its original unencrypted state
  301.   we simple xor the encrypted value with 'b' again:
  302.  
  303.   int new;
  304.   new = encrypted ^ b;
  305.   which gives us this binary number: 01100001 which is, you guesses it, 'a'!
  306.  
  307.   So now we know the basic component behind the encryption of characters.
  308.   Continue on to learn how to encrypt a whole file of strings, numbers etc.
  309.  
  310.   1.3.3 Structured C Programming
  311.  
  312.   When writing code in any language it is important to write clear, 
  313. efficient
  314.   code that is well documented and easy to read, here are some tips:
  315.  
  316.   1: To make readability of code easier, #define values at the start
  317.      of the file that can be reused throughout the code, here's an example:
  318.      #define SIZE 5
  319.      int array[SIZE] = {3,4,5,6,7};
  320.   2: Try to use pointers wherever possible, observe:
  321.   /* Pathetic version, recopies itself, inefficient */
  322. ---------------------------------------------
  323.   main()
  324.   {
  325.     int z;
  326.      z = 10;
  327.       func(z); // create copy of z two times.
  328.   }
  329.   int func(int value)
  330.   {
  331.    return = value * value; // return the square of a number
  332.   }
  333. ----------------------------------------------
  334.  
  335. /* Good version, simply refers to memory locations, and does not copy
  336.     variables, this is named, call by reference whereas the first version
  337.     was call by value
  338. */
  339. ----------------------------------------------
  340. main()
  341. {
  342.   int z = 10;
  343.   func(&10);     // reference z twice, efficient.
  344. }
  345. void func(unsigned *value)
  346. {
  347.    *value *= *value;
  348. }
  349. ----------------------------------------------
  350.  
  351. 3: Allocate Memory Correctly:
  352.  
  353.   You could allocate 1000 of bytes like this, but you cannot free the
  354.   used memory.
  355.  
  356.   int array[1000];
  357.  
  358.   Try doing this, it really works!
  359.  
  360.   int *value;
  361.   value = (int *) malloc(sizeof(int) * 4);
  362.   free(value);
  363.  
  364.   Wow, efficient code, finally!
  365.  
  366. 4: Indent your god damn programs!, please!
  367.  
  368. Instead of this:
  369.  
  370. int array[] = {3434,4545,34534,656,
  371. 23434,3454,45345,34535,
  372. 234,345345,345345,345,2435,3434,
  373.                      34,345,566,5};
  374.    for(i = 0; i <= SIZE -1; i++ ) {
  375. sum += array[i] ;
  376. // add some more unreadable stuff...
  377. well this aint that good of example but its badly structured, observ the
  378. same code in structured format:
  379.  
  380. int array[] = { 3434,4545,34534,656,
  381.                  23434,3454,45345,
  382.                  34535,234,345345,
  383.                  345345,345,2435,
  384.                  3434,34,345,566,5};
  385.   for (i = 0; i <= SIZE -1; i++) {
  386.     sum += array[i];
  387.      //indent
  388.       // some more
  389.        //
  390.       //
  391.      //
  392.     //
  393.      //
  394.       //
  395.        //
  396.         //
  397.          // keep it up your going good!
  398.  
  399. You can clearly see what I am on about, and be sure to fix small things like
  400. this, instead of
  401.  
  402. while(!a=EOF)func1(val,val2,val3){i++;j++;g--;}
  403. do this:
  404. while (!a = EOF)
  405.    func1(val, val2, val3) {
  406.      i++; // increment i;
  407.      j++; // dont add unnecessary comments
  408.      g--;
  409.          }
  410.  
  411. 4: Ok nearly there, one last thing, add a signature, date etc.
  412.  
  413. There's nothing better then seeing a great program, and seeing the authors
  414. name and contact so you can email them on there great efforts, BE MODEST!
  415. ACCEPT ALL RECOGNITION FOR YOUR masterpiece. A good signature is as follows:
  416. /******************************************
  417. NAME:  John Johnson
  418. EMAIL: john@cybernet.com
  419. ICQ:   35614954
  420. DATE:  10/9/2000
  421. *******************************************/
  422.  
  423. Wooooo I know its a big chunk to absorb, but absorb it damnit!
  424. Continue onto the next section, now that your efficient... :)
  425.  
  426. 9.9.9 Encryption Example
  427.   As I promised here's a budget example :)
  428.   Remember the XOR operator ^ ?
  429.   Xor operator == ^ in C :)
  430.   Were going to write a small documented, structured program that will
  431.   encrypt a file with the users key, the file can then be decrypted
  432.   with the key. Otherwise the resulting file is encrypted again.
  433.   The Encryption in this program is very weak, but to explain more complex
  434.   algorithmic is too hard for now, maybe in my next article :)
  435.  
  436.   First, I will go over some features of this program that need be 
  437. understood
  438.   for all you wanna be crypotologists out there!
  439.  
  440.   Heres the code that loops through a file:
  441.   while (c = getc(*file) != EOF) { encrypt() putc(*file,c) }
  442.   // loop through *file until End of File is reached, each time
  443.   // encrypting each character and incrementing it to the next character in
  444.   // the file stream.
  445.  
  446.   Here's the algorithm Im using:
  447.  
  448.   c = c ^ *file2;
  449.   counter++;
  450.  
  451.   That's it?
  452.   Indeed it is, very small, very powerful..
  453.   Without further do, I represent a simple encryption program:
  454.  
  455.   -------------------------------------------
  456.   /******************************************************
  457.   Standard XOR Encryption By Axion
  458.   UIN  :          35614954
  459.   EMAIL:          axionis@hotmail.com
  460.   DATE :          MAY 21st 2000
  461.   USAGE:          xe filein fileout key
  462.   *******************************************************/
  463.  
  464. #include <stdio.h>
  465.  
  466. #define PROG_NAME "xe"
  467. void usage()
  468. {
  469. printf("-------------------------------\n");
  470. printf("Invalid command line.\n");
  471. printf("Usage:\n\t%s infile outfile key\n", PROG_NAME);
  472. printf("-------------------------------\n");
  473. }
  474.  
  475.  
  476. int main(int argc, char *argv[])
  477. {
  478.       int count,bytes; /* counter when looping through file, and bytes to 
  479. count filesize */
  480.       FILE *in,*out;   /* In and out FILE Streams to read/write data */
  481.  
  482.        if(argc < 4) {  /* Error check the command line */
  483.         usage();       /* Display Usage Information on error */
  484.          return 0;     /* Exit Program returning 0 - no error */
  485.          }
  486.  
  487.           if (( in = fopen(argv[1], "rb")) == NULL) /* Error Check File Streams 
  488. */
  489.               {
  490.                    printf("Error opening %s.\n", argv[1]);
  491.                   }
  492.                       if (( out = fopen(argv[2], "wb")) == NULL)
  493.                       {
  494.                            printf("Error opening %s.\n", argv[2]);
  495.                       }
  496.  
  497.  
  498.                  while(( count = getc(in)) != EOF)
  499.            {
  500.               count = count ^ *argv[3]; /* Apply XOR  ( KEY ) */
  501.             bytes++;             /* Increment counter of filesize */
  502.           putc(count, out);        /* Write new file */
  503.          }
  504.  
  505.          fclose(in);
  506.              fclose(out);
  507.            printf("Encryption Success:\n");
  508.             printf("\tEncrypted %s and stored data in %s.\n", 
  509. argv[1],argv[2]);
  510.              printf("\tWrote %d bytes to %s.\n", bytes,argv[2]);
  511.    return 0;
  512. }
  513. /*  To compile under djgpp */
  514. /*  dgjpp xe.c -o xe       */
  515. -------------------------------------------
  516.  
  517.  
  518. Well that's it, my tutorial is almost complete..!
  519. But not without a few more challenges, exercises for the smart people:
  520. 1. Write an encryptor that uses one complimentary instead of Xor.
  521. 2. Write a program that uses two keys on a file.
  522. 3. Write a program that uses no logical operators ?
  523. HINT, refer to an ASCII character set.
  524.   Lets assume, *s points to ASCII value 'a'.
  525.   *s -= 31;
  526.   turns it from 'a' to 'A'
  527.   This should be enough to start you off on your quest.  If anyone has any 
  528. comments
  529.   or would like to report any bugs write to axionis@hotmail.com
  530.   or ICQ me on #35614954!
  531.   Bye from me, hope to see you at the top.
  532.  
  533.  
  534.  
  535.  
  536.  
  537.  
  538.  
  539.  
  540.